<?php

namespace App\Http\Controllers\Laifei\Settlement;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class ProxyDetailController extends Controller
{
    /**
     * 查询代理用户信息
     */
    public function index()
    {

        $all = $this->all;
        $we_id = $all['we_id'];

        // 用户信息
        $message = DB::table('proxy_users as pu')
            ->leftJoin('lottery_users as lu', 'pu.we_id', '=', 'lu.we_id')
            ->where('pu.we_id', '=', $we_id)
            ->select('lu.openid', 'lu.nickname', 'lu.phone', 'pu.proxy_id', 'pu.proxy_code', 'pu.wallet')
            ->get()
            ->toArray();
        $message = json_decode(json_encode($message), true);
        $message = $message[0];
        $message['wallet'] = $message['wallet'] / 100.00;

        // 提现金额
        $total_withdraw = DB::table('proxy_log')
            ->where('proxy_we_id','=',$we_id)
            ->where('type','=',0)
            ->sum('proxy_price');
        $message['total_withdraw'] = $total_withdraw / 100.00;

        $total = DB::table('proxy_log')
            ->where('proxy_we_id','=',$we_id)
            ->where('type','<>',0)
            ->sum('proxy_price');
        $message['total'] = $total / 100.00;

        // 代理用户信息
        $list = DB::table('lottery_users as lu')
            ->leftJoin('proxy_log as pl', 'lu.we_id', '=', 'pl.consumer_we_id')
            ->where('lu.proxy', '=', $we_id)
//            ->where('lu.nickname','<>','')
            ->select('lu.we_id', 'lu.openid', 'lu.nickname')
            ->selectRaw('COUNT(DISTINCT `pl`.`log_id`) as count, SUM(CASE WHEN pl.type <> 0 THEN pl.proxy_price ELSE 0 END) as proxy_price')
            ->groupBy('lu.we_id')
            ->get()
            ->map(function ($item) {
                $item->proxy_price = $item->proxy_price / 100.00;
                if($item->proxy_price){
                    $item->children = [];
                }
                return $item;
            })
            ->toArray();

        // 提现
        $withdraw = DB::table('proxy_log')
            ->where('proxy_we_id', '=', $we_id)
            ->where('type','=',0)//提现
            ->select('log_id','mch_billno','proxy_price','created_at')
            ->orderByDesc('created_at')
            ->get()
            ->map(function ($item) {
                $item->proxy_price = $item->proxy_price / 100.00;
                return $item;
            })
            ->toArray();

        return $this->setReturnJson(0, 'success', ['message' => $message, 'list' => $list, 'withdraw' => $withdraw]);

    }


    public function log()
    {
        $all = $this->all;

        $we_id = $all['we_id'];

        $result = DB::table('proxy_log')
            ->where('consumer_we_id','=',$we_id)
            ->select('log_id','type','payment','proxy_price','created_at')
            ->orderByDesc('log_id')
            ->get()
            ->map(function($item){
                switch ($item->type){
                    case 0:
                        $item->type_name = '提现';
                        break;
                    case 1:
                        $item->type_name = '黑卡会员';
                        break;
                    case 2:
                        $item->type_name = '课程';
                        break;
                    case 3:
                        $item->type_name = '探店';
                        break;
                    case 4:
                        $item->type_name = '霸王餐';
                        break;
                }
                $item->payment = $item->payment / 100.00;
                $item->proxy_price = $item->proxy_price / 100.00;
                return $item;
            })
            ->toArray();

        return $this->setReturnJson(0,'success',$result);

    }

}
